/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//comment

import java.util.Date;
import java.util.Calendar;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.swing.JOptionPane;

/*
 * HolidayInterface.java
 *
 * Created on 23-Feb-2012, 15:01:21
 */

/**
 *
 * @author higgina0
 */
public class HolidayInterface extends javax.swing.JFrame {

    private int driverID;
    /** Creates new form HolidayInterface */
    public HolidayInterface(int driverID) {
        this.driverID = driverID;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        startDateField = new javax.swing.JFormattedTextField(new java.text.SimpleDateFormat("MMM dd, yyyy"));
        endDateField = new javax.swing.JFormattedTextField(new java.text.SimpleDateFormat("MMM dd, yyyy"));
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        daysLeftField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setBackground(new java.awt.Color(227, 217, 215));

        jLabel1.setText("Start Date: ");

        jLabel2.setText("End Date: ");

        startDateField.setValue(new java.util.Date());

        endDateField.setText(null);
        endDateField.setValue(new java.util.Date());

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        jLabel3.setText("Days Left:");

        daysLeftField.setText(Integer.toString(25 - (DriverInfo.getHolidaysTaken(this.driverID))));
        daysLeftField.setEnabled(false);

        jLabel4.setFont(new java.awt.Font("DejaVu Sans", 1, 14)); // NOI18N
        jLabel4.setText("Request Holiday Form");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18))
                            .addComponent(jLabel2))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(35, 35, 35))
                            .addComponent(endDateField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
                            .addComponent(startDateField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(daysLeftField, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(52, 52, 52))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(114, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addContainerGap(246, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(startDateField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(daysLeftField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(41, 41, 41)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(endDateField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        // TODO add your handling code here:
        //First check date is not less than current
        DateFormat formatter = new SimpleDateFormat("MMM dd, yyyy");
        try
        {
            //Parse date from the given java text field
            Date start = (Date)formatter.parse(startDateField.getText());
            Calendar currentDate = Calendar.getInstance();
            currentDate.set(Calendar.MINUTE, 0);
            currentDate.set(Calendar.HOUR, 0);
            currentDate.set(Calendar.SECOND, 0);
            currentDate.add(Calendar.DATE, -1);

            Date end = (Date)formatter.parse(endDateField.getText());

            Date today = currentDate.getTime();
            Calendar checkStart = Calendar.getInstance();
            Calendar checkEnd = Calendar.getInstance();

            checkStart.setTime(start);
            checkEnd.setTime(end);

            //System.out.println("Start date:" + checkStart.get(Calendar.MILLISECOND));
            //System.out.println("Current: " + currentDate.get(Calendar.MILLISECOND));
            //System.out.println("End date: " + checkEnd);

            long startMil = checkStart.getTimeInMillis();
            long endMil = checkEnd.getTimeInMillis();

            //Get the number of days holiday lasts for
            long durationDays = (endMil - startMil) / (24 * 60 * 60 * 1000);
            durationDays += 1;

            if (start.before(today))
                ErrorGui.showError("Error!", "Start date is before today!");
            else if(end.before(start))
                ErrorGui.showError("Error!", "End date is before Start date!");
            else if(end.before(today))
                ErrorGui.showError("Error!", "End date is before today!");

            else
            {
                //Create holiday
                Holiday driverHoliday = 
                        new Holiday(start, (int)durationDays, this.driverID);
                //Set holiday
                if (driverHoliday.setHoliday())
                {
                    JOptionPane.showMessageDialog(null, "Holiday successful!");
                    int newDaysLeft = driverHoliday.calculateDaysLeft();
                    System.out.println("Days left: " + newDaysLeft);  
                    daysLeftField.setText(Integer.toString(newDaysLeft));

                }

            }

        }
        catch (ParseException e)
        {
            ErrorGui.showError("Error!", "Could not parse date!");
        }

    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        int result = JOptionPane.showConfirmDialog(this, "Are you sure?", "Confirmation", JOptionPane.YES_NO_OPTION);
        
        if (result == 0)
            this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

   /* @Override
    public void dispose()
    {
        //database.busDatabase.close();
        super.dispose();
*/
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new HolidayInterface(2014).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField daysLeftField;
    private javax.swing.JFormattedTextField endDateField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JButton okButton;
    private javax.swing.JFormattedTextField startDateField;
    // End of variables declaration//GEN-END:variables

}
